<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Admin_login extends CI_Controller {

    function Admin_login() {
        parent :: __construct();
    }

    public function index() {
        $this->session->sess_destroy();
        $this->load->view('admin/admin_login.php');
    }

    function admin_logout() {
        $this->session->sess_destroy();
        redirect('admin/admin_login');
    }

    public function submit() {
        if ($this->input->post('login')) {
            //print "Subh";
            $this->session->unset_userdata('message');
            $this->form_validation->set_rules('admin_user', 'admin_user', 'required|xss_clean');
            $this->form_validation->set_rules('admin_password', 'admin_password', 'required|xss_clean');

            if ($this->form_validation->run()) {
                $this->db->where('user', $this->input->post('admin_user', TRUE));
                $this->db->where('password', $this->input->post('admin_password', TRUE));
                $this->db->where('status', '51');
                $query = $this->db->get('admin_user');

                if ($query->num_rows() == 1) {
                    $row = $query->row();
                    $data = array
                        (
                        'last_login' => unix_to_human(time(), TRUE),
                        'last_ip' => $this->input->ip_address()
                    );
                    //--Update last_login n IP
                    $this->db->where('user', $this->input->post('admin_user', TRUE));
                    $this->db->update('admin_user', $data);

                    // Our user exists, set session.
                    $this->session->set_userdata('sess_admin_user', $row->user);
                    $this->session->set_userdata('sess_admin_password', $row->password);
                    $this->session->set_userdata('sess_admin_name', $row->name);
                    $this->session->set_userdata('sess_admin_last_login', $row->last_login);
                    $this->session->set_userdata('sess_admin_last_ip', $row->last_ip);
                    $this->session->set_userdata('sess_admin_status', $row->status);

                    redirect('admin/dashboard');
                } else {
                    $this->session->set_userdata('message', 'Error! Invalid Login..');
                    $this->load->view('admin/admin_login.php');
                }
            } else {
                $this->session->set_userdata('message', 'Error! Invalid Login..');
                $this->load->view('admin/admin_login.php');
            }
        }
    }

}

?>